package com.cloud.drore.eboss.ebossmfile.controller;

import com.cloud.drore.eboss.ebossmfile.base.BusinessException;
import com.cloud.drore.eboss.ebossmfile.base.ResultMessage;
import com.cloud.drore.eboss.ebossmfile.fastdfs.FastDFSClient;
import com.cloud.drore.eboss.ebossmfile.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.io.*;
import java.util.Arrays;

/**
 * 浙江卓锐科技股份有限公司 版权所有 © Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/2/8 15:03 <br>
 * @Author: <a href="836327318@qq.com">吴昌益</a>
 */
@RestController
@Api(value = "/文件上传", description = "文件上传-吴昌益")
public class UploadController {

    @Resource
    private FastDFSClient fastDFSClient;

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(UploadController.class);

    @GetMapping("/")
    public String index() {
        return "eboss-upload";
    }

    /**
     * @param file
     * @param redirectAttributes
     * @ClassName: UploadController
     * @Return: String
     * @Decription:
     * @CreateDate: Created in 2018/2/8 15:47
     * @Author: <a href="836327318@qq.com">吴昌益</a>
     * @Modify:
     */
    @PostMapping("/uploadImage")
    @ApiOperation(value = "图片上传")
    public ResultMessage uploadImg(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) throws BusinessException, IOException {

        if (file.isEmpty()) {
            throw new BusinessException("上传文件不能为空");
        }
    /*    InputStream inputStream = file.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"GBK"); //亮点在这
        BufferedReader in = new BufferedReader(inputStreamReader);
        StringBuffer buffer = new StringBuffer();
        String line ;
        while ((line = in.readLine()) != null){
            buffer.append(line);
        }
        System.out.println(buffer);*/

        String path;
        try {
            // Get the file and save it somewhere
            path = fastDFSClient.uploadFile(file);
            return ResultMessage.Builder.builder().withData(path).build();
        } catch (Exception e) {
            logger.error("upload file failed", e);
        }
        throw new BusinessException("上传文件失败！！");
    }
    @PostMapping("/uploadFile")
    @ApiOperation(value = "文件上传")
    public ResultMessage singleFileUpload(@RequestParam("file") MultipartFile file,
                                          RedirectAttributes redirectAttributes) throws BusinessException {

        if (file.isEmpty()) {
            throw new BusinessException("上传文件不能为空");
        }
        String path = "";
        try {
            // Get the file and save it somewhere
            path = fastDFSClient.uploadFile(file);
            return ResultMessage.Builder.builder().withData(path).build();
        } catch (Exception e) {
            logger.error("upload file failed", e);
        }
        throw new BusinessException("上传文件失败！！");
    }

}
